<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_model extends CI_Model 
{

	function __construct()
	{
		parent::__construct();
		
	}
	
	
	// GET ORDER FUNCTION
	function getOrders($query=FALSE,$limit=FALSE,$offset=FALSE)
	{
		$this->db->order_by('order_id','desc');
		if($query=='all'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='neworder'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','neworder');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='confirmed'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','confirmed');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='pending'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','pending');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='shipped'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','shipped');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='closed'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','closed');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='trashed'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_status','trashed');
			$this->db->order_by('order_id','desc');
			return $this->db->get('tb_order')->result_array();
		}
		return $this->db->get('tb_order')->result_array();
	}
	
	
	// GET ORDER CARI
	function getCariOrder($query=FALSE,$limit=FALSE,$offset=FALSE,$code=FALSE,$nama=FALSE)
	{
		if(!$query){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_code',$code);
			$this->db->like('tb_member.nama',$nama);
			$this->db->order_by('order_date','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='code'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->where('order_code',$code);
			$this->db->order_by('order_date','desc');
			return $this->db->get('tb_order')->result_array();
		}
		elseif($query=='nama'){
			$this->db->limit($limit,$offset);
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->like('tb_member.nama',$nama);
			$this->db->order_by('order_date','desc');
			return $this->db->get('tb_order')->result_array();
		}

	}
	
	// GET ORDER BY ID CUSTOM
	function getOrderById($the_id=FALSE,$id=FALSE,$mem_id=FALSE){
		
		if ($the_id == 'order_id')
		{
			/*$query = $this->db->where('tb_order.order_id',$id)
						->order_by('tb_order.order_date','asc')
						->join('tb_member','tb_member.member_id = tb_order.member_id')
						->join('tb_member','tb_member.member_id = tb_order.member_id')
						->get('tb_order');*/
			
			$query = $this->db->query("SELECT m.member_id, m.nama, m.email, m.telp,m.hp,m.alamat,m.kota, m.kodepos, o.order_bank_id, o.order_status, o.total, o.order_id, o.order_code, o.order_date, p.nama_provinsi, k.nama_kabupaten
			FROM tb_order o
			JOIN tb_member m ON m.member_id = o.order_member_id
			JOIN tb_province p ON p.prov_id = m.mem_prov_id
			JOIN tb_kab k ON k.kab_id = m.mem_kab_id
			WHERE o.order_id = $id
			ORDER BY o.order_date DESC");
			return $query->row();
			
		}
		elseif ($the_id == 'member_id')
		{
			$query = $this->db->query("SELECT m.member_id, m.nama, m.email, m.telp,m.hp,m.alamat,m.kota, 
			m.kodepos, o.order_bank_id, o.order_status,o.order_logistik_kirim_id,o.total, o.order_id, 
			o.order_code, o.order_date, p.nama_provinsi, k.nama_kabupaten,
			b.nama_rek,b.nama_bank,b.no_rek
			
			FROM tb_order o
			JOIN tb_member m ON m.member_id = o.order_member_id
			JOIN tb_province p ON p.prov_id = m.mem_prov_id
			JOIN tb_kab k ON k.kab_id = m.mem_kab_id
			
			JOIN tb_bank b ON b.bank_id = o.order_bank_id
			WHERE m.member_id = $mem_id
			ORDER BY o.order_date DESC");
			return $query->result_array();
		}
		
		elseif ($the_id == 'all_where_mem_id')
		{
			$query = $this->db->query("SELECT o.order_id,p.nama_produk,p.harga,d.det_jml,d.harga,d.det_subtotal
				FROM tb_order_detail d
				JOIN tb_produk p ON p.produk_id = d.produk_id
				JOIN tb_order o ON o.order_id = d.order_id
				JOIN tb_member m ON m.member_id = o.order_member_id
				WHERE o.order_id = $id
				ORDER BY o.order_date DESC");
			return $query->result_array();
		}
		
	}


	// EDIT DETAIL ORDER
	function editOrderDetail($order_id,$order_det_id,$jmlNew,$prod_id){
		// select order_detail
		$this->db->where('order_detail_id',$order_det_id);
		$row = $this->db->get('tb_order_detail')->row();
		// select produk  to get harga nya
		$this->db->where('produk_id',$prod_id);
		$row_prod = $this->db->get('tb_produk')->row();
		// select order  to get totalnya 
		$this->db->where('order_id',$order_id);
		$row_order = $this->db->get('tb_order')->row();
		
		$totOld = $row_order->total; // total lama
		
		
		
		$kurangi = $totOld - $row->det_subtotal; //total lama dikurangi subtotal lama detail produk yg di edit
		
		
		
		// update tb_detail_ordernya
	
		//if persen_diskon nya ada
		if($row_prod->persen_diskon !=0):
			
			$harga_disk = $row_prod->harga - (($row_prod->harga * $row_prod->persen_diskon) / 100);
			$subTot = $harga_disk * $jmlNew;// sub total baru
			
			//update total ordernya di tb_order
			$totNew = $kurangi + $subTot;
			$this->db->set('total', $totNew, FALSE);
			$this->db->where('order_id',$order_id);
			$this->db->update('tb_order');
			
			$this->db->set('harga',$harga_disk, FALSE);
			$this->db->set('det_jml', $jmlNew, FALSE);
			$this->db->set('det_subtotal', $subTot, FALSE);
			$this->db->where('order_detail_id',$order_det_id);
			$this->db->update('tb_order_detail');
		else:
		//if persen_diskon = 0
			//echo $row_prod->harga;
			$subTot = $row_prod->harga * $jmlNew;// sub total baru
			
			$totNew = $kurangi + $subTot;
			$this->db->set('total', $totNew, FALSE);
			$this->db->where('order_id',$order_id);
			$this->db->update('tb_order');
			
			$this->db->set('harga', $row_prod->harga, FALSE);
			$this->db->set('det_jml', $jmlNew, FALSE);
			$this->db->set('det_subtotal', $subTot, FALSE);
			$this->db->where('order_detail_id',$order_det_id);
			$this->db->update('tb_order_detail');
		endif;
		
		//jumlah stok produknya
		$stokOld = $row_prod->jml_stok;
		
		// jumlah order lama
		$jmlOld = $row->det_jml;
		
		
			
		if ($jmlOld > $jmlNew): // jika jumlah yg lama lebih besar dari jumlah baru
		
			$itungJml = $jmlOld - $jmlNew; // maka jumlah lama akan dikurangi jumlah baru
			$stokNew =  $stokOld + $itungJml; // sisa akan ditambahkan ke stok
			
			$this->db->set('jml_stok', $stokNew, FALSE);
			$this->db->where('produk_id',$prod_id);
			$this->db->update('tb_produk');
			
		elseif ($jmlOld < $jmlNew): // jika jumlah yg lama lebih kecil dr yg baru
		
			$itungJml = $jmlNew - $jmlOld; // maka jumlah baru akan dikurangi jumlah lama
			$stokNew =  $stokOld - $itungJml;// stok akan dikurangi
			
			$this->db->set('jml_stok', $stokNew, FALSE);
			$this->db->where('produk_id',$prod_id);
			$this->db->update('tb_produk');
		else:
			return FALSE;
		endif;	
	}
	
	
	// GET ORDER BY
	function getOrderBy($query=FALSE,$order_id=FALSE,$code=FALSE)
	{
		if ($query=='by_id'):
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id','left outer');
			$this->db->join('tb_province','tb_province.prov_id = tb_order.order_prov_id','left outer');
			$this->db->join('tb_kab','tb_kab.kab_id = tb_order.order_kab_id','left outer');
			$this->db->join('tb_bank','tb_bank.bank_id = tb_order.order_bank_id','left outer');
			//$this->db->join('tb_logistik_kirim','tb_logistik_kirim.logistik_kirim_id = tb_order.order_logistik_kirim_id','left outer');
			//$this->db->join('tb_logistik','tb_logistik.logistik_id = tb_logistik.logistik_id','left outer');
			$this->db->where('order_id',$order_id);
			$query = $this->db->get('tb_order');
			return $query->row();
		elseif ($query=='by_code'):
			$this->db->join('tb_member','tb_member.member_id = tb_order.order_member_id');
			$this->db->join('tb_province','tb_province.prov_id = tb_order.order_prov_id');
			$this->db->join('tb_kab','tb_kab.kab_id = tb_order.order_kab_id');
			$this->db->join('tb_bank','tb_bank.bank_id = tb_order.order_bank_id');
			//$this->db->join('tb_logistik','tb_logistik.logistik_id = tb_order.order_logistik_kirim_id');
			$this->db->where('en_code',$code);
			$query = $this->db->get('tb_order');
			return $query->row();
		endif;
		

	}

	
	// GET ORDER DETAIL BY ID ORDER
	function getOrderDetailById($orderid)
	{
		$this->db->where('order_id',$orderid);
		$this->db->join('tb_produk','tb_produk.produk_id = tb_order_detail.produk_id');
        $this->db->join('tb_ms_size','tb_order_detail.prod_size_id = tb_ms_size.ms_size_id');
		$query = $this->db->get('tb_order_detail');
		return $query->result_array();
	}

	

	   
	// ADD ORDER
	function addOrder($data){
	
		$this->db->insert('tb_order', $data);
	}
	
	// ADD ORDER DETAIL
	function addOrderDetail($data){
	
		$this->db->insert('tb_order_detail',$data);
	
	}

	// UPDATE ORDER
	function editOrder($id,$data){
		$this->db->where('order_id',$id);
		$this->db->update('tb_order',$data);
	}

	// DELETE ORDER
	function deleteOrder($id){
		$this->db->where('order_id',$id);
		$this->db->delete('tb_order');
	}
	
	// DELETE ORDER DETAIL
	function deleteOrderDetail($id){
		$this->db->where('order_id',$id);
		$this->db->delete('tb_order_detail');
	}

	// DELETE TEMP CART
	function deleteTempCart($session_id){
		$this->db->where('session_id',$session_id);
		$this->db->delete('tb_temp_cart');
	}
	
	
	// FUNC TO SUM TOTAL
	function subTotal($produk_id,$jml)// func itung total
	{
		$this->db->select('harga');
		$this->db->select('persen_diskon');
		$this->db->where('produk_id',$produk_id);
		$query = $this->db->get('tb_produk');
		$row = $query->row();
		if($row->persen_diskon != 0):
			$disc_price = getPriceDiskon($row->harga,$row->persen_diskon);
			$subTotal = $jml * $disc_price; //itung totalnya
			return $subTotal;
		else:
			$subTotal = $jml * $row->harga; //itung totalnya
			return $subTotal;
		endif;
		
	}
	
	
	// ADD TO TEMP CART
	function addToCart($session_id,$produk_id,$harga,$disc,$jml,$size_id,$date){
		//cek product id dan sessionnya
		$this->db->where('session_id',$session_id);
		$this->db->where('temp_produk_id',$produk_id);
        $this->db->where('temp_size_produk_id',$size_id);
		$query = $this->db->get('tb_temp_cart');
		$cart = $query->result_array();
		$num_cart = $query->num_rows();
		//jika product id sudah ada dengan ukuran sama
		if($num_cart>0) 
		{
                    if($disc != 0):
							$disc_price = getPriceDiskon($harga,$disc);
                            $this->db->set('temp_harga', $disc_price, FALSE);
                            $this->db->set('temp_jml', 'temp_jml+'.$jml, FALSE);
                            $this->db->set('temp_size_produk_id',$size_id, FALSE);
                            $this->db->set('temp_subtotal', 'temp_subtotal+'.$this->subTotal($produk_id,$jml), FALSE);
                            $this->db->where('session_id',$session_id);
                            $this->db->where('temp_produk_id',$produk_id);
							$this->db->where('temp_size_produk_id',$size_id);
                            $this->db->update('tb_temp_cart');
                    else:
                            $this->db->set('temp_harga', $harga, FALSE);
                            $this->db->set('temp_size_produk_id',$size_id, FALSE);
                            $this->db->set('temp_jml', 'temp_jml+'.$jml, FALSE);
                            $this->db->set('temp_subtotal', 'temp_subtotal+'.$this->subTotal($produk_id,$jml), FALSE);
                            $this->db->where('session_id',$session_id);
                            $this->db->where('temp_produk_id',$produk_id);
							$this->db->where('temp_size_produk_id',$size_id);
                            $this->db->update('tb_temp_cart');
                    endif;
                        

		}
		//jika product id belum ada
		else
		{
			if($disc != 0):
				$disc_price = getPriceDiskon($harga,$disc);
				$data = array(
				   'temp_produk_id' => $produk_id,
                   'temp_size_produk_id' => $size_id,
				   'temp_harga' => $disc_price,
				   'temp_jml' => $jml,
				   'session_id' => $session_id,
				   'created' => $date,
				   'temp_subtotal' => $this->subTotal($produk_id,$jml),
				);
				$this->db->insert('tb_temp_cart', $data);
			else:
				$data = array(
				   'temp_produk_id' => $produk_id,
                   'temp_size_produk_id' => $size_id,
				   'temp_harga' => $harga,
				   'temp_jml' => $jml,
				   'session_id' => $session_id,
				   'created' => $date,
				   'temp_subtotal' => $this->subTotal($produk_id,$jml),
				);
				$this->db->insert('tb_temp_cart', $data);
			endif;
		}

		//delete old files
		//$this->db->where('created <',$date);
		//$this->db->delete('tb_temp_cart');

	}

	
	// UPDATE CART
	function editCart($session_id,$produk_id,$harga,$disc,$jml,$size_id,$subtotal){
		$this->db->where('session_id',$session_id);
		$this->db->where('temp_produk_id',$produk_id);
		$this->db->where('temp_size_produk_id',$size_id);
		$query = $this->db->get('tb_temp_cart');
		$cart = $query->result_array();
		$num_cart = $query->num_rows();
		$date_order_date = date("Y-m-d");
		//jika product id sudah ada
		if($num_cart>0) {
			if($disc != 0):
				$disc_price = getPriceDiskon($harga,$disc);
				//$this->db->set('temp_harga', $disc_price, FALSE);
				$this->db->set('temp_jml', $jml, FALSE);
				$this->db->set('temp_subtotal', $subtotal, FALSE);
				$this->db->where('session_id',$session_id);
				$this->db->where('temp_produk_id',$produk_id);
				$this->db->where('temp_size_produk_id',$size_id);
				$this->db->update('tb_temp_cart');
			else:
				//$this->db->set('temp_harga', $harga, FALSE);
				$this->db->set('temp_jml', $jml, FALSE);
				$this->db->set('temp_subtotal', $subtotal, FALSE);
				$this->db->where('session_id',$session_id);
				$this->db->where('temp_produk_id',$produk_id);
				$this->db->where('temp_size_produk_id',$size_id);
				$this->db->update('tb_temp_cart');
			endif;
		}
	}

	// DELETE CART
	function deleteCart($session_id,$produk_id,$size_prod_id){
		$this->db->where('session_id',$session_id);
		$this->db->where('temp_produk_id',$produk_id);
		$this->db->where('temp_size_produk_id',$size_prod_id);
		$this->db->delete('tb_temp_cart');
	}

	// GET CART
	function getCart($session_id,$produk_id=FALSE,$num_rows=FALSE,$temp_size=FALSE){
		if($produk_id) {
			$this->db->where('session_id',$session_id);
			$this->db->where('temp_produk_id',$produk_id);
                        $this->db->where('temp_size_produk_id',$temp_size);
			$this->db->join('tb_produk','tb_produk.produk_id = tb_temp_cart.temp_produk_id');
                        $this->db->join('tb_size_produk','tb_size_produk.size_produk_id = tb_temp_cart.temp_size_produk_id');
			$query = $this->db->get('tb_temp_cart');
		}
		else {
                        
			$this->db->where('session_id',$session_id);
                        $this->db->join('tb_produk','tb_produk.produk_id = tb_temp_cart.temp_produk_id');
                        $this->db->join('tb_size_produk','tb_size_produk.size_produk_id = tb_temp_cart.temp_size_produk_id');
			$query = $this->db->get('tb_temp_cart');
		}
		$cart = $query->result_array();
		$num_cart = $query->num_rows();
		if($num_rows) {
			return $num_cart;
		}
		else {
			if($cart) {
				foreach($cart as $no => $val){
					//product data
					$this->db->where('produk_id',$val['temp_produk_id']);
					$product_detail = $this->db->get('tb_produk');
					$cart[$no]['product'] = $product_detail->result_array();
					//product image
					$this->db->where('produk_id',$val['temp_produk_id']);
					$product_image = $this->db->get('tb_produk');
					$cart[$no]['image_url'] = $product_image->result_array();
				}
				return $cart;
			}
		}
	}
	
	// GET WISHLIST
	function getWishlist($mem_id){
		$this->db->where('tb_wishlist.member_id',$mem_id);
		$this->db->join('tb_produk','tb_produk.produk_id = tb_wishlist.produk_id');
		$this->db->join('tb_brand','tb_brand.brand_id = tb_produk.prod_brand_id');
		$this->db->select('tb_produk.nama_produk,tb_brand.nama_brand,tb_produk.produk_id,tb_produk.prod_brand_id,tb_wishlist.date_added');
		$qr = $this->db->get('tb_wishlist')->result_array();
		
		return $qr;
	
	}
	
	
	// ADD WISHLIST
	
	function addWishlist($data){
		$this->db->insert('tb_wishlist',$data);
	
	}
	
	// EDIT WISHLIST
	
	
	// DELETE WISHLIST
	
	
	
	
	// CONFIRMATION PROCESS
	
	// ADD CONFIRMATION
	function addConfirmation($data){
		$this->db->insert('tb_konfirmasi',$data);
	}
	
	// EDIT CONFIRMATION
	
	
	// GET CONFIRMATION
	

}
